<?php

namespace Apps\AppApi\Method\User;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserFollow;

/**
 * 关注用户/专家
 */
class Follow extends BaseMethod
{
    public function run()
    {
    	if(false == $this->verifyUserAuth()){
    		return false;
    	}
    	$user_id = intval($this->getDataItem('user_id', 0));
    	$to_user_id = intval($this->getDataItem('to_user_id', 0));
    	$type = intval($this->getDataItem('type', 0));

		if ($user_id == 0 || $to_user_id == 0 || $type == 0) {
			$this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
			return false;
		}
		if (!in_array($type, ['1', '2'])) {
			$this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, 'type类型错误！');
			return false;
		}

		$UserFollow = new UserFollow();
		if ($UserFollow->is_follow($user_id, $to_user_id, $type)) {
			$this->setResult(['success' => 3, 'message' => '已经关注过了']);
		} else { // 未关注
			if ($UserFollow->follow($user_id, $to_user_id, $type)) {

				if ($type == 2) {// 增加专家的粉丝数
					(new \Apps\Common\Models\UserBase())->setExpertFollowCount($to_user_id, 1, true);
				}
				$this->setResult(['success' => 1, 'message' => '关注成功']);
			} else {
				$this->setResult(['success' => 2, 'message' => '关注失败']);
			}
		}
    }


}